package com.liugui.thread;

/**
 * @ClassName: DeadLockDemo
 * @Author: liugui
 * @Date: 2020-11-09 10:32
 **/
public class DeadLockDemo {
    private static Object lock1 = new Object();
    private static Object lock2 = new Object();

    public static void main(String[] args) {
        //模拟死锁  使其相互等待
        //这里使用lambda表达式创建了一个线程
        //线程  1
        new Thread(() -> {
            synchronized (lock1) {
                try {
                    //线程休眠一段时间  确保另外一个线程可以获取到b锁
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("D");
                synchronized (lock2) {
                }
            }
        }).start();
        //线程 2
        new Thread(() -> {
            synchronized (lock2) {
                System.out.println("死锁...");
                synchronized (lock1) {
                }
            }
        }).start();
    }
}
